home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Sample Controls / BDiamond / C3DTextView.cp < prev    next >
Encoding:
Text File  |  1996-12-20  |  4.4 KB  |  177 lines  |  [TEXT/CWIE]

  1. #include "ocheaders.h"
  2. #include "FnAssert.h"
  3. #include "Colors.h"
  4. #include "C3DView.h"
  5. #include "C3DTextView.h"
  6.  
  7. //------------------------------------------------------------------------------
  8.  
  9. C3DTextView::C3DTextView() : C3DView()
  10. {
  11.     mText[0]        = 0;
  12.     
  13.     mTextColor        = RGB_BLACK;
  14.     
  15.     mTextLocation.v    = 0;
  16.     mTextLocation.h    = 0;
  17. }
  18.  
  19. //------------------------------------------------------------------------------
  20.  
  21. void
  22. C3DTextView::Draw3D(const DrawContext * context)
  23. {
  24.     inherited::Draw3D(context);
  25.     
  26.     Boolean isDeep = CheckScreenDepth(context) > 1;
  27.     
  28.     //    Adjust for "button" position.  If the button is depressed (poor, sad
  29.     //    little button), we move the button down and to the right by one pixel,
  30.     //    to help make it look like the button was actually pressed down 
  31.     //    (unless we're only one-bit deep).
  32.     
  33.     Point textLocation = mTextLocation;
  34.     if ( isDeep && IsDepressed() ) 
  35.     {
  36.         (textLocation.h)++;
  37.         (textLocation.v)++;
  38.     }
  39.     
  40.     ::MoveTo(textLocation.h, textLocation.v);
  41.     
  42.     ::PenState savePen;
  43.     ::GetPenState(&savePen);
  44.     ::PenNormal();
  45.     
  46.     RGBColor theForeColor;
  47.     GetTextColor(&theForeColor);
  48.     ::RGBForeColor(&theForeColor);
  49.     
  50.     SetFontParams();
  51.     
  52.     ::DrawString(mText);
  53.     
  54.     ::SetPenState(&savePen);
  55. }
  56.  
  57. //------------------------------------------------------------------------------
  58.  
  59. void
  60. C3DTextView::SetTextColor(const DrawContext * context, const RGBColor & textColor, Boolean redraw)
  61. {
  62.     ASSERT(context != NULL, "NULL draw context!");
  63.     
  64.     if ( ! RGBColorsEQ(textColor, mTextColor) )
  65.     {
  66.         mTextColor = textColor;
  67.         
  68.         if ( redraw )
  69.             Draw3D(context);
  70.     }
  71. }
  72.  
  73. //------------------------------------------------------------------------------
  74.  
  75. void
  76. C3DTextView::GetTextColor(RGBColor * textColor)
  77. {
  78.     *textColor = mTextColor;
  79. }
  80.  
  81. //------------------------------------------------------------------------------
  82.  
  83. Boolean
  84. C3DTextView::PlaceText(const DrawContext * context, const StringPtr text)
  85. {
  86.     ASSERT(context != NULL, "NULL draw context!");
  87.     ASSERT(text != NULL, "NULL text!");
  88.     
  89.     Boolean textFits = true;
  90.     
  91.     //    Calculate the text location (mTextLocation).  This is the "home"
  92.     //    location, and may actually be modified in the Draw method, based
  93.     //    on IsDepressed().
  94.  
  95.     //    Calculate the horizontal and vertical sizes.
  96.         
  97.     SetFontParams();
  98.     short textWidth = ::StringWidth(text);
  99.     
  100.     FontInfo fontInfo;
  101.     ::GetFontInfo(&fontInfo);
  102.     short textHeight = fontInfo.ascent + fontInfo.descent;
  103.     
  104.     //    Now compute mTextLocation to center the text in the view, with some 
  105.     //    possible minor adjustments based on fState, to make the "button"
  106.     //    look like it's being pushed down and let up.
  107.  
  108.     Rect extent = context->Location;
  109.     
  110.     //    horizontally...
  111.     long leftOverH = (extent.right - extent.left) - textWidth;
  112.     textFits = textFits && leftOverH > 0;
  113.     leftOverH = leftOverH < 0 ? 0 : leftOverH; // limit, in case text is too wide
  114.     mTextLocation.h = (short)(extent.left + (leftOverH / 2));
  115.     
  116.     //    ... and vertically...
  117.     long leftOverV = (extent.bottom - extent.top) - textHeight;
  118.     textFits = textFits && leftOverV > 0;
  119.     leftOverV = leftOverV < 0 ? 0 : leftOverV; // limit, in case text is too tall
  120.     mTextLocation.v = (short) 
  121.         (extent.bottom - fontInfo.descent - (leftOverV / 2) - 1); 
  122.                           // Note that the -1 on the end is because 
  123.                         // normally the text looks a little
  124.                         // too low when it's "properly" centered,
  125.                         // so we move it up a pixel.  
  126.     
  127.     return textFits;
  128. }
  129.  
  130. //------------------------------------------------------------------------------
  131.  
  132. void
  133. C3DTextView::SetText ( const DrawContext * context, const char * text, Boolean redraw )
  134. {
  135.     ASSERT(context != NULL, "NULL draw context!");
  136.     ASSERT(text != NULL, "NULL text!");
  137.     
  138.     Str255 inputText;
  139.     if ( strlen(text) > 255 ) // MMF: use a constant here!
  140.         strncpy((char *)inputText, text, 255);  // MMF: use a constant here!
  141.     else
  142.         strcpy((char *)inputText, text);
  143.         
  144.     c2pstr((char *)inputText);
  145.     
  146.     if ( PLstrcmp(inputText, mText) ) // if it's NOT the same text
  147.     {
  148.         PLstrcpy(mText, inputText);
  149.         
  150.         Boolean textFits = PlaceText(context, mText);
  151.         
  152.         if ( redraw && textFits )
  153.             Draw3D(context);
  154.     }
  155. }
  156.  
  157. //------------------------------------------------------------------------------
  158.  
  159. void
  160. C3DTextView::GetText ( char * text )
  161. {
  162.     ASSERT(text != NULL, "NULL input pointer!");
  163.     
  164.     PLstrcpy((StringPtr)text, mText);
  165.     p2cstr((StringPtr)text);
  166. }
  167.  
  168. //------------------------------------------------------------------------------
  169. void        
  170. C3DTextView::SetFontParams(void)
  171. {
  172.     ::TextFace(bold);
  173.     ::TextFont(applFont);
  174.     ::TextSize(12);
  175.     ::TextMode(srcCopy);
  176. }    
  177.